home *** CD-ROM | disk | FTP | other *** search
/ PC Player 2004 May / pc player 2004-05.iso / Demos / FarCry / Data1.cab / _007F643649184A408DDB1A5E7411607C < prev    next >
Encoding:
Text File  |  2004-01-06  |  6.3 KB  |  182 lines

  1. -- AI_CombatManager
  2. -- version 1.0 Amanda 2002-11-18 based on AI_BoredManager
  3. --
  4. -- look for an anchor appropriate to the AI type within specified range, 
  5. -- if find an anchor return identifying string, anchor type and signal otherwise return nil
  6. -- for Combat AI this can be used to signal anchor appropriate behavior
  7. --
  8. -- parameters 
  9. --    enity.id, AI type(constant), range(int)
  10. -- returns 
  11. --    found(string name of anchor), anchorType(constant), signal(string name of goal pipe to call)
  12. --------------------------------------------------------------------------------------------------------------------------------------------
  13. --eg.
  14. --    local combatAnchor = AI_CombatManager:FindAnchor(entity,AI_CombatManager.AICOMBAT_SCIENTIST,20);
  15. --    if (combatAnchor) then
  16. --        entity:SelectPipe(0,combatAnchor.signal,combatAnchor.found);
  17. --    end
  18. --------------------------------------------------------------------------------------------------------------------------------------------
  19.  
  20. AI_CombatManager = {
  21.     -- define values for AI_Type
  22.     AICOMBAT_GUARD = 1,
  23.     AICOMBAT_SWAT = 2,
  24.     AICOMBAT_SCIENTIST = 3,
  25. }
  26. --anchor may have one or more associated signals
  27. AI_CombatManager.options = {
  28.     [AI_CombatManager.AICOMBAT_SWAT ] = {
  29.  
  30.          {anchorType = AIAnchor.AIANCHOR_SHOOTSPOT,  tag = 0, probability = 160, signal = "Shootspot",}, 
  31.          --standing attack to the left
  32.          {anchorType = AIAnchor.AIANCHOR_LEFT, tag = 0, probability = 140,
  33.              manner = {
  34.                 {signal = "swat_comeout_left",  probability = 500}, 
  35.                 {signal = "swat_comeout_rollleft",  probability = 300}, 
  36.                 {signal = "swat_kneelattack",  probability = 200}, 
  37.                 --{signal = "throw_grenade",  probability = 200}, 
  38.                 },
  39.         }, 
  40.         --standing attack to the right
  41.          {anchorType = AIAnchor.AIANCHOR_RIGHT, tag = 0, probability = 140,
  42.              manner = {
  43.                 {signal = "swat_comeout_right",  probability = 500}, 
  44.                 {signal = "swat_comeout_rollright",  probability = 300}, 
  45.                 {signal = "swat_kneelattack",  probability = 200}, 
  46.                 --{signal = "throw_grenade",  probability = 200}, 
  47.                 },
  48.         }, 
  49.         --standing attack to the left in a confined space eg. too tight to roll
  50.         {anchorType = AIAnchor.AIANCHOR_LEFT_TIGHT, tag = 0, probability = 140,
  51.              manner = {
  52.                 {signal = "ComeOut_Left",  probability = 500}, 
  53.                 },
  54.         }, 
  55.         --standing attack to the right in a confined space
  56.          {anchorType = AIAnchor.AIANCHOR_RIGHT_TIGHT, tag = 0, probability = 140,
  57.              manner = {
  58.                 {signal = "ComeOut_Right",  probability = 500}, 
  59.                 },
  60.         },     
  61.         --attack from crouch to the left        
  62.         {anchorType = AIAnchor.AIANCHOR_CROUCH_LEFT, tag = 0, probability = 140,
  63.             manner = {
  64.                 --{signal = "Crouch_LeanLeft",  probability = 250}, 
  65.                 {signal = "swat_comeout_rollleft",  probability = 200}, 
  66.                 {signal = "swat_crouchcomeout_left",  probability = 400}, 
  67.                 {signal = "swat_standup",  probability = 400}, 
  68.                 --{signal = "Crouch_ThrowGrenade",  probability = 300}, 
  69.                 },
  70.         },
  71.         --attack from crouch to the right
  72.         {anchorType = AIAnchor.AIANCHOR_CROUCH_RIGHT, tag = 0, probability = 140,
  73.             manner = {
  74.                 {signal = "swat_comeout_rollright",  probability = 200}, 
  75.                 {signal = "swat_crouchcomeout_right",  probability = 400}, 
  76.                 {signal = "swat_standup",  probability = 400}, 
  77.                 --{signal = "Crouch_ThrowGrenade",  probability = 300}, 
  78.                 },
  79.         },
  80.     }, 
  81.     [AI_CombatManager.AICOMBAT_SCIENTIST ] = {
  82.          {anchorType = AIAnchor.AIANCHOR_GUN_RACK,  tag = 0, probability = 100, signal = "scientist_grabGun",}, 
  83.          {anchorType = AIAnchor.AIANCHOR_PUSH_ALARM,  tag = 0, probability = 350, signal = "scientist_PushAlarm",}, 
  84.          {anchorType = AIAnchor.AIANCHOR_PULL_ALARM,  tag = 0, probability = 350, signal = "scientist_PullAlarm",}, 
  85.           {anchorType = AIAnchor.AIANCHOR_TABLE,  tag = 0, probability = 200, 
  86.               manner ={ {signal = "scientist_table_crouch",  probability = 500}, 
  87.                                  {signal = "scientist_table_prone",  probability = 500}, 
  88.               }
  89.           }, 
  90.     }
  91. }    
  92.  
  93. function AI_CombatManager:FindAnchor(entity,AI_Type,range)
  94.  
  95.     --set default values
  96.     local foundObject = nil;
  97.     local option = {};
  98.     local idx = 0;
  99.     self.tagCount = 0;
  100.     
  101.     --check valid AI_type
  102.     if (self.options[AI_Type]) then
  103.         local countOptions = (getn(self.options[AI_Type]));    
  104.         self:ResetTags(AI_Type);
  105.     
  106.         -- take weighted random anchor option for this AI type, tag options that return not found
  107.         -- continue until you have tried all available options
  108.          while ((foundObject == nil) and (self.tagCount < countOptions)) do
  109.              idx = self:WeightedChoice(AI_Type);
  110.              if (idx) then
  111.                  option = self.options[AI_Type][idx];
  112.                  --foundObject = AI:FindObjectOfType(entity:GetPos(),range,option.anchorType);
  113.                  foundObject = AI:FindObjectOfType(entity.id,range,option.anchorType);
  114.                  if (foundObject == nil) then
  115.                       option.tag = 1;
  116.                      self.tagCount = self.tagCount + 1; 
  117.                  end
  118.              end
  119.          end
  120.          
  121.          if (foundObject) then 
  122.              -- if there is only one signal send that otherwise choose from set of options
  123.              if (option.signal) then
  124.                  return {found = foundObject, anchorType = option.anchorType, signal = option.signal};
  125.              else
  126.                  idx = self:ChooseManner(option.manner);
  127.                  if (idx) then
  128.                      return {found = foundObject, anchorType = option.anchorType, signal = option.manner[idx].signal};    
  129.                  end
  130.          end
  131.      
  132.          else
  133.              return nil;
  134.          end
  135.      else
  136.          System:Warning("["..entity:GetName().."] AI_CombatManager+++++++++++++No behaviours have been defined for this AI type");
  137.          return nil;
  138.      end
  139. end
  140.  
  141. function AI_CombatManager:ResetTags(AI_Type)
  142.     for index,value in self.options[AI_Type] do
  143.         value.tag = 0;
  144.     end
  145. end
  146.  
  147. function AI_CombatManager:WeightedChoice(AI_Type)
  148. -- choose option based on probability
  149.  
  150.     local total = 0;
  151.     local rnd = random(1,1000);
  152.     
  153.     for index, value in self.options[AI_Type] do
  154.         -- check that option has not already been tried
  155.         if (value.tag == 0) then
  156.             total = total + value.probability;
  157.             if (rnd < total) then
  158.                 return  index;
  159.             end
  160.         end        
  161.     end 
  162.  
  163.     --return found;
  164.     return nil;
  165. end
  166.  
  167.  
  168. function AI_CombatManager:ChooseManner(manner)
  169. -- choose manner based on probability
  170.  
  171.     local total = 0;
  172.     local rnd = random(1,1000);
  173.     
  174.     for index, value in manner do
  175.         total = total + value.probability;
  176.         if (rnd < total) then
  177.              return index;
  178.         end    
  179.     end 
  180.  
  181.     return nil;
  182. end